import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
// import Menu from '@/components/Menu'
// import Admin from '@/components/Admin'
// import About from '@/components/About/About'
// import Login from '@/components/Login'
// import Register from '@/components/Register'
// import Music from '@/components/Music'


// 二级路由
import Contact from '@/components/About/Contact'
import History from '@/components/About/History'
import OrderInfo from '@/components/About/OrderInfo'
import Comments from '@/components/About/Comments'

// 三级路由
import Email from '@/components/About/contact/Email'
import Phone from '@/components/About/contact/Phone'

Vue.use(Router)

export default new Router({
  routes: [
    {
      name:'homeLink',
      path: '/home',
      components:{
        default:Home,
        History,
        OrderInfo,
        Comments
      }
    },
    {
      name: '/menuLink',
      path: '/menu',
      component: resolve=>require(['@/components/Menu'],resolve)
    },
    {
      name:'adminLink',
      path: '/admin',
      component: resolve=>require(['@/components/Admin'],resolve),
      // 路由独享的守卫
      beforeEnter:(to,from,next) => {
        console.log(window.localStorage._userName);
        if (!window.localStorage._userName) {
          if (confirm("此页面只有管理员才能访问，现在登陆吗？")) {
            next("/login");
          }
        }else{
          next();
        }
        // if (to.path==="/login"||to.path==="/regist") {
        //   next();
        // }else{
        //   alert("你还没登录！");
        //   next("/login");
        // }
      }
    },
    {
      path: '/about',
      component: resolve=>require(['@/components/About/About'],resolve),
      redirect:'/about/history',
      children:[
         {path:'/about/contact',name:'contactLink',component:Contact,redirect:'/about/contact/email',children:[
            {path:'/about/contact/email',name:'email',component:Email},
            {path:'/about/contact/phone',name:'phone',component:Phone},
         ]},
         {path:'/about/history',name:'historyLink',component:History},
         {path:'/about/orderInfo',name:'orderInfoLink',component:OrderInfo},
         {path:'/about/comments',name:'commentsLink',component:Comments},
      ]
    },
    {
      name:'loginLink',
      path: '/login',
      meta: {
        keepAlive: false // 不需要缓存
      },
      component: resolve=>require(['@/components/Login'],resolve),
      beforeEnter:(to,from,next) => {
        if (window.localStorage._userName) {
          alert("您已经登陆了！");
          window.history.back();
        }else{
          next();
        }
      }
    },
    {
      name:'registLink',
      path: '/regist',
      component: resolve=>require(['@/components/Register'],resolve),
      beforeEnter:(to,from,next) => {
        if (window.localStorage._userName) {
          alert("您已经注册过了！");
          window.history.back();
        }else{
          next();
        }
      }
    },
    {
      name: '/musicLink',
      path: '/music',
      component: resolve=>require(['@/components/Music'],resolve)
    },
    {
      path:'*',
      redirect:'/home'
    }
    
  ],
  // mode:'history',
  scrollBehavior(to,from,savedPosition){
    // return {x:0,y:100}
    // return { selector:'.btn' }
    if (savedPosition) {
      return savedPosition;
    }else{
      return {x:0,y:0};
    }
  },
})